const {
  http
} = require("../../../../utils/apiutils");
const
  util = require("../../../../utils/util")

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    latitude:0,
    longitude:0,
  },
  search:function(e){
     console.log("--------------",e.detail)
     if(e.detail.length>0)
     {
      this.jobsearch(e.detail)
     }
     else
     {
       wx.showToast({
         title: '输入内容后检索',
         icon:"none",
       })

     }

  },
  jobsearch:function(keyword){
    var data = {
      "keyword": keyword //手机号码
    }
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.jobsearch(data,
      function (res) {
        wx.hideLoading()
        if (res.code == 0) {
          var arr = res.data;
          arr.forEach(element => {
            // let ds = util.getDistance(that.data.latitude, that.data.longitude, element.lat, element.lng);
            // console.log("-----------", ds)
            // if (ds > 1) {
            //   element.ds = ds.toFixed(1) + "公里"
            // } else {
            //   element.ds = (ds * 1000).toFixed(0) + "米"
            // }
            if (!that.data.latitude == 0) {
              let ds = util.getDistance(that.data.latitude, that.data.longitude, element.lat, element.lng);
              console.log("-----------", ds)
              if(element.stationFollow==1)
              {
                element.dst = 0
              }
              else
              {
                element.dst = ds;
              }
              if (ds > 1) {
                element.ds = ds.toFixed(1) + "公里"
              } else {
                element.ds = (ds * 1000).toFixed(0) + "米"
              }
            } else {
              element.ds = "未知"
              element.dst = 0;
            }
          });
          arr = arr.sort(compare("dst"))
          that.setData({
            dataList: arr,
          })
        
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.isGetSetting()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: getApp().globalData.userInfo.inviteCode?'/pages/index/index?code='+getApp().globalData.userInfo.inviteCode:'/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  },
  onclickCell: function (e) {
    if (!getApp().globalData.isLogin) {
      wx.showModal({
        title: "提示",
        content: "您还没有登录，登陆后即可查看详情",
        confirmText: "去登陆",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login?ptype=home',
            })
          }
        }
      })
      return;
    }
    let item = e.currentTarget.dataset.item;
    // if (index % 2 == 0) {
    //   wx.navigateTo({
    //     url: '/pages/home/pages/train/train',
    //   })
    // } else {
    wx.navigateTo({
      url: '/pages/home/pages/work/work?id=' + item.id,
    })
    // }
  },
  isGetSetting() {
    // 判断用户是否授权
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.data.latitude = res.latitude;
        that.data.longitude = res.longitude;
        console.log("-----用户位置信息--", res)
        that.getJob();
      },
      fail: function (res) {
        wx.showToast({
          title: '您没有开启定位服务无法计算距离',
          icon: "none"
        })
        that.getJob();
      }
    })
    return
  }
})
function compare(property) {
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    return value1 - value2;
  }
}